package com.googlecode.ddplan.dwr.domain;

import com.googlecode.ddplan.PDDLAction;
import com.googlecode.ddplan.PDDLName;
import com.googlecode.ddplan.PDDLState;
import com.googlecode.ddplan.PDDLType;

@PDDLType
public class Robot {
	@PDDLName
	private String name;

	private Box box;

	public Robot(String name) {
		this.name = name;
	}

	public void take(Box box) {
		this.box = box;
	}

	public void release() {
		this.box = null;
	}

	@PDDLState
	public boolean at(Location location) {
		return location.hasRobot(this);
	}

	@PDDLState
	public boolean unloaded() {
		return box == null;
	}

	@PDDLState
	public boolean loaded(Box box) {
		if (unloaded()) {
			return false;
		}
		return this.box.equals(box);
	}

	@PDDLAction(precond = { "$1.adjacent($2)", "!$2.occupied", "at($1)" }, effects = { "at($2)", "$2.occupied", "!$1.occupied", "!at($1)" })
	public void move(Location locBefore, Location locAfter) {
		if (!canMove(locBefore, locAfter)) {
			throw new IllegalArgumentException("Unable to move robot!");
		}
		locBefore.freeRobot();
		locAfter.enterRobot(this);
	}

	private boolean canMove(Location locBefore, Location locAfter) {
		return locBefore.adjacent(locAfter) && !locAfter.occupied() && at(locBefore);
	}

}
